package com.example.userservice.controller;


import com.example.userservice.common.RestResponse;
import com.example.userservice.model.User;
import com.example.userservice.service.UserService;
import org.bouncycastle.cert.ocsp.Req;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-06-04
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger  LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

   //-------------------查询---------------------


    @RequestMapping("/getusername")
    public  RestResponse<String> getUsername(Long id) {
        try {
            Thread.sleep(100000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LOGGER.info("get username");
        return RestResponse.success("test success");
    }

    @RequestMapping("/getById")
    public RestResponse<User> getUserById(Long id) {
        User user = userService.getUserById(id);
        return RestResponse.success(user);
    }

    @RequestMapping("/getList")
    public RestResponse<List<User>> getUserList(@RequestBody User user) {
        List<User> users = userService.getUserByQuery(user);
        return RestResponse.success(users);
    }


    //----------------------注册----------------------------------
    @RequestMapping("/add")
    public RestResponse<User> add(@RequestBody User user) {
        userService.addAccount(user,user.getEnableUrl());
        return RestResponse.success();
    }


    /**
     * 主要激活key的验证
     */
    @RequestMapping("/enable")
    public RestResponse<Object> enable(String key){
        userService.enable(key);
        return RestResponse.success();
    }

    //------------------------登录/鉴权--------------------------

    @RequestMapping("/auth")
    public RestResponse<User> auth(@RequestBody User user) {
        User finalUser = userService.auth(user.getEmail(),user.getPasswd());
        return RestResponse.success(finalUser);
    }

    @RequestMapping("/get")
    public RestResponse<User> getUser(String token){
        User finalUser = userService.getLoginedUserByToken(token);
        return RestResponse.success(finalUser);
    }

    @RequestMapping("/logout")
    public RestResponse<Object> logout(String token) {
        userService.invalidate(token);
        return RestResponse.success();
    }
}




